package com.leostadyn.stackfarm.timestatistics.view.activity;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.model.bean.LoginBean;
import com.leostadyn.stackfarm.timestatistics.presenter.LoginPresenter;
import com.leostadyn.stackfarm.timestatistics.utils.ButtonUtils;
import com.leostadyn.stackfarm.timestatistics.utils.ToastUtil;
import com.leostadyn.stackfarm.timestatistics.view.activity.admin.ManagementActivity;
import com.leostadyn.stackfarm.timestatistics.view.activity.employee.EmployeeActivity;
import com.leostadyn.stackfarm.timestatistics.view.vinterface.ILoginView;
import com.orhanobut.hawk.Hawk;

import static android.text.InputType.TYPE_CLASS_TEXT;
import static android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD;
import static android.text.InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;

public final class LoginActivity extends MVPActivity<ILoginView, LoginPresenter> implements ILoginView, View.OnClickListener {

    private EditText accountEdit;
    private EditText passwordEdit;
    private Button loginBtn;
    private ProgressBar progressBar;
    private ImageView eyeImg;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        checkHawk();
    }

    private void initView(){
        accountEdit = findViewById(R.id.edit_account);
        passwordEdit = findViewById(R.id.edit_password);
        loginBtn = findViewById(R.id.btn_login);
        progressBar = findViewById(R.id.progressBar);
        eyeImg = findViewById(R.id.img_eye);

        loginBtn.setOnClickListener(this);
        eyeImg.setOnClickListener(this);
    }

    void checkHawk(){
        String account=Hawk.get("account",null);
        String password=Hawk.get("password",null);
        if(account!=null&&password!=null){
            accountEdit.setText(account);
            passwordEdit.setText(password);
            login(account,password);
        }
    }

    boolean checkEditTextLength(){
        int accountLength=accountEdit.getText().toString().length();
        int passwordLength=passwordEdit.getText().toString().length();
        if(accountLength<=30){
            return passwordLength >= 6 && passwordLength <= 16;
        }
        return false;
    }

    void login(String account, String password){
        mPresenter.Judge(account, password, compositeDisposable);
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void showWaiting() {
        accountEdit.setEnabled(false);
        passwordEdit.setEnabled(false);
        loginBtn.setBackground(getDrawable(R.drawable.btn_login_busy));
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void dismissWaiting() {
        accountEdit.setEnabled(true);
        passwordEdit.setEnabled(true);
        loginBtn.setBackground(getDrawable(R.drawable.btn_login_normal));
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void disconnected() {
        ToastUtil.show(LoginActivity.this, getResources().getString(R.string.login_failure));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                if (TextUtils.isEmpty(accountEdit.getText()) || TextUtils.isEmpty(passwordEdit.getText())) {
                    ToastUtil.show(this, getString(R.string.please_fill_in_the_username_or_password));
                }
                if (TextUtils.isEmpty(accountEdit.getText())) {

                } else {
                    if(checkEditTextLength()) {
                        if (!ButtonUtils.isFastDoubleClick(R.id.btn_login))
                            login(accountEdit.getText().toString(), passwordEdit.getText().toString());
                    }else {
                        ToastUtil.show(LoginActivity.this, getResources().getString(R.string.account_or_password_length_no_match));
                    }
                }
                break;

            case R.id.img_eye:
                if(passwordEdit.getInputType()==(TYPE_CLASS_TEXT|TYPE_TEXT_VARIATION_PASSWORD)){
                    eyeImg.setImageResource(R.mipmap.visible);
                    passwordEdit.setInputType(TYPE_CLASS_TEXT|TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    passwordEdit.setTypeface(Typeface.DEFAULT);
                }
                else{
                    eyeImg.setImageResource(R.mipmap.invisible);
                    passwordEdit.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD);
                    passwordEdit.setTypeface(Typeface.DEFAULT);
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void successMessage(LoginBean loginBean) {

            Hawk.put("account", accountEdit.getText().toString());
            Hawk.put("password", passwordEdit.getText().toString());
            Hawk.put("token", loginBean.getToken());
        if (loginBean.isAdmin()) {
                Intent intent = new Intent(LoginActivity.this, ManagementActivity.class);
                startActivity(intent);
        } else if (!loginBean.isAdmin()) {
                Intent intent = new Intent(LoginActivity.this, EmployeeActivity.class);
                startActivity(intent);
            }
            finish();

    }
    @Override
    public void failureMessage(String s) {
        if (s.equals("fail_auth_check")) {
            ToastUtil.show(this, "用户名或密码错误");
        } else
            ToastUtil.show(LoginActivity.this, s);
    }

}
